/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.component;

import java.io.StringWriter;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLWriter;

/**
 *
 * @author Lunayo
 */
public class HTMLDocumentPlusRemove {
    public static void removeElement(HTMLDocument d, Element e) throws Exception {
        Element p = e.getParentElement();
        int n = p.getElementCount();
        String s = "";
        for (int i = 0; i < n; i++) {
            Element c = p.getElement(i);
            if (c != e) {
                s += getElementText(d, c);
            }
        }
        d.setInnerHTML(p, s);
    }   
    
    public static String getElementText(HTMLDocument d, final Element e) throws Exception {
        StringWriter sw = new StringWriter();
        int p0 = e.getStartOffset();
        int p1 = e.getEndOffset();
        new HTMLWriter(sw, d, p0, p1 - p0) {
            @Override
            protected ElementIterator getElementIterator() {
                return new ElementIterator(e);
            }
        }.write();
        return sw.toString();
    }
}
